package com.example.servicelearn;

import java.io.UnsupportedEncodingException;

public class DataUtils {
    public static final String TAG = "DataUtils";


    /**
     * 异或加密
     *
     * @param arr     需要加密的数据
     * @param keyWord 秘钥
     * @return 加密后的数据
     * @throws UnsupportedEncodingException
     */
    public static byte[] xoEncrypt(byte[] arr, String keyWord) throws UnsupportedEncodingException {
        byte[] keyarr = keyWord.getBytes("UTF-8");

        byte[] result = new byte[arr.length];
        for (int i = 0; i < arr.length; i++) {
            result[i] = (byte) (arr[i] ^ keyarr[i % keyarr.length]);
        }
        return result;
    }

    /**
     * 异或加密解密
     *
     * @param text    需要解密数据
     * @param keyWord 秘钥
     * @return 解密后的数据
     * @throws UnsupportedEncodingException 异常
     */
    public static byte[] xorDecrypt(byte[] text, String keyWord) throws UnsupportedEncodingException {
        byte[] keyarr = keyWord.getBytes("UTF-8");
        for (int i = 0; i < text.length; i++) {
            text[i] = (byte) (text[i] ^ keyarr[i % keyarr.length]);
        }
        return text;
    }

}
